package com.yb.web.goods;

import java.io.File;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import com.yb.base.web.BaseAction;
import com.yb.bussiness.global.SystemLogService;
import com.yb.bussiness.global.SystemLogServiceImpl;
import com.yb.bussiness.goods.ProductService;
import com.yb.bussiness.goods.ProductServiceImpl;
import com.yb.bussiness.shoper.BrandService;
import com.yb.bussiness.shoper.BrandServiceImpl;
import com.yb.domain.goods.TgProduct;
import com.yb.util.comm.Contants;
import com.yb.util.comm.RequestUtil;
import com.yb.util.file.FileUtil;
import com.yb.util.file.XmlUtil;
import com.yb.util.framework.PageUtil;

public class ProductAction extends BaseAction {
	private ProductService service;
	private int page;
	private List rows;
	private int total;
	private TgProduct product;
	private File imgFile;
	private String imgFileFileName;
	private SystemLogService systemLogService;
	private BrandService brandservice;

	public ProductAction() {
		service = new ProductServiceImpl();
		systemLogService = new SystemLogServiceImpl();
		brandservice = new BrandServiceImpl();
	}

	public String search() {
		try {
			int page = RequestUtil.getParam(request, "page", 1);
			int limit = RequestUtil.getParam(request, "rows", 10);
			String name = RequestUtil.getParam(request, "name", "");
			Long brandId = RequestUtil.getParam(request, "bid", 0l);
			PageUtil pu = service.findByBrandId(brandId, page, limit, name);
			rows = pu.getList();
			total = pu.getTotalRecords();
		} catch (Exception e) {
			log.error("查找商家产品出错", e);
		}
		return Contants.JSONSTRING_RESULT_NAME;
	}

	/**
	 * 新增、修改活动 案例
	 * 
	 * @return
	 */
	public String showInfo() {
		Long bid = RequestUtil.getParam(request, "bid", 0l);
		Long id = RequestUtil.getParam(request, "id", 0l);
		if (0l != id)
			product = service.read(Long.valueOf(id));
		if (null == product) {
			product = new TgProduct();
			product.setBrandId(bid);
		}
		return "showInfo";
	}

	/**
	 * 保存产品
	 * 
	 * @return
	 */
	public String saveInfo() {
		try {
			if (null != product) {
				if (null != imgFile) {
					String basePath = request.getSession().getServletContext()
							.getRealPath("/");
					String compressPath = XmlUtil.getValue("imgPath",
							"sysConfig")
							+ "/";
					String fileName = "";
					fileName = new Date().getTime()
							+ imgFileFileName.substring(imgFileFileName
									.lastIndexOf("."));
					if (FileUtil.doUpFile(basePath + compressPath + fileName,
							imgFile)) {
						product.setImgsrc(compressPath + fileName);
					}
				}
				if(product.getId() == 0l){
					product.setId(null);
					product.setCreateTime(new Date());
				}
				systemLogService.saveSystemLog(request, "商家产品管理", "新增或修改了产品信息--"+brandservice.findById(product.getBrandId().toString()).getName());
				service.save(product);
			}
		} catch (Exception e) {
			log.error("保存商家产品出错", e);
		}
		return "saveSuccess";
	}

	public String deleteByIds() {
		PrintWriter out = null;
		StringBuffer sb = null;
		try {
			out = response.getWriter();
			sb = new StringBuffer("[");
			String idString = RequestUtil.getParam(request, "idString", "");
			String[] delteid = idString.split("_");
			try {
				for (int i = 0; i < delteid.length; i++) {
					systemLogService.saveSystemLog(request, "商家产品管理", "删除产品信息--"+brandservice.findById(service.read(Long.valueOf(delteid[i])).getBrandId().toString()).getName());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			service.deleteByIds(idString);
			sb.append("{\"message\":\"删除成功\"" + "}");
			sb.append("]");
			out.println(sb.toString());
		} catch (Exception e) {
			sb.append("{\"message\":\"删除失败\"" + "}");
			sb.append("]");
			out.println(sb.toString());
			log.error("删除商家产品出错", e);
		}
		return null;
	}

	public ProductService getService() {
		return service;
	}

	public void setService(ProductService service) {
		this.service = service;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public List getRows() {
		return rows;
	}

	public void setRows(List rows) {
		this.rows = rows;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	public TgProduct getProduct() {
		return product;
	}

	public void setProduct(TgProduct product) {
		this.product = product;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

}
